﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Model;
using TrainingsApp.Logic;
using ZJFramework10.Extension;

namespace TrainingsApp.IPADApp.Master {
    public partial class TeachMaterialsEdit : PageBase {


        public TS_TeachMaterials_Entity MaterialsEntity;


        public long MaterialsId {
            get {
                return ZJFramework10.Utility.QueryStringManager.GetValue<int>("mid");
            }
        }

        protected void Page_Load(object sender, EventArgs e) {



            cbxGrade.DataSource = TrainingsApp.Logic.TS_Grade_Logic.ListAll();
            cbxGrade.ValueField = "Idx";
            cbxGrade.ValueType = typeof(int);
            cbxGrade.TextField = "GradeName";
            cbxGrade.DataBind();
            if (MaterialsId > 0) {

                MaterialsEntity = TS_TeachMaterials_Logic.TS_TeachMaterials_View(MaterialsId);



                if (!this.aspTree.IsNewNodeEditing) {
                    List<TS_TeachMaterials_Chapter_Entity> dataSource = TrainingsApp.Logic.TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_ListByMaterialsId(MaterialsId);

                    if (dataSource == null || dataSource.Count <= 0) {
                        var rootEntity = TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_Add(
                               new TS_TeachMaterials_Chapter_Entity {
                                   IsLeaf = true,
                                   MaterialsId = MaterialsEntity.Idx,
                                   Name = MaterialsEntity.Name,
                                   ParentIdx = 0,
                                   RootPath = string.Empty,
                                   Code = "RRRRRR",
                                   IsDelete = 0
                               });

                        dataSource.Add(rootEntity);
                    }

                    this.aspTree.DataSource = dataSource;
                    this.aspTree.DataBind();
                }

            }
            else {
                aspTree.Visible = false;
            }


            this.cbxCourseInfo.DataSource = TrainingsApp.Logic.TS_CourseInfo_Logic.TS_CourseInfo_ListByPaging(100, 1).TS_CourseInfo_List;
            this.cbxCourseInfo.ValueField = "Idx";
            cbxCourseInfo.ValueType = typeof(int);
            this.cbxCourseInfo.TextField = "CourseName";
            this.cbxCourseInfo.DataBind();
            if (!IsPostBack) {
                if (MaterialsEntity != null) {
                    this.cbxCourseInfo.Value = MaterialsEntity.CourseId.Value;
                    this.cbxGrade.Value = MaterialsEntity.Grade;
                    this.txtName.Text = MaterialsEntity.Name;
                    this.txtCide.Text = MaterialsEntity.Code;

                }
            }


        }


        private void DataBinds() {

            List<TS_TeachMaterials_Chapter_Entity> dataSource = TrainingsApp.Logic.TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_ListByMaterialsId(1);
            this.aspTree.DataSource = dataSource;
            this.aspTree.DataBind();
        }

        protected void aspTree_NodeInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {

            string text = e.NewValues["Name"] as string;
            string code = e.NewValues["Code"] as string;
            long parentIndex = Convert.ToInt64(e.NewValues["ParentIdx"].ToString());
            TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_Add(
                       new TS_TeachMaterials_Chapter_Entity {
                           IsLeaf = true,
                           MaterialsId = MaterialsEntity.Idx,
                           Name = text,
                           ParentIdx = parentIndex,
                           RootPath = string.Empty,
                           Code = code,
                           IsDelete = 0
                       });

            DataBinds();
        }

        protected void aspTree_NodeInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void aspTree_NodeUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
            long idx = Convert.ToInt64(e.Keys["Idx"]);
            var updateEntity = TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_View(idx);
            string text = e.NewValues["Name"] as string;
            string code = e.NewValues["Code"] as string;

            updateEntity.Name = text;
            updateEntity.Code = code;

            TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_Update(updateEntity);

            DataBinds();
        }

        protected void aspTree_NodeUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void aspTree_NodeDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
            long idx = Convert.ToInt64(e.Keys["Idx"]);
            TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_Delete(idx);
            DataBinds();
        }

        protected void aspTree_NodeDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void aspTree_CancelNodeEditing(object sender, DevExpress.Web.ASPxTreeList.TreeListNodeEditingEventArgs e) {
            DataBinds();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            if (MaterialsEntity != null) {
                MaterialsEntity.Name = this.txtName.Text;
                MaterialsEntity.CourseId = (int)this.cbxCourseInfo.Value;
                MaterialsEntity.Grade = this.cbxGrade.Value.AsInt();
                MaterialsEntity.Code = this.txtCide.Text;
                TS_TeachMaterials_Logic.TS_TeachMaterials_Update(MaterialsEntity);
            }
            else {
                MaterialsEntity = new TS_TeachMaterials_Entity();
                MaterialsEntity.Name = this.txtName.Text;
                MaterialsEntity.Code = this.txtCide.Text;
                MaterialsEntity.CourseId = (int)this.cbxCourseInfo.Value;
                MaterialsEntity.Grade = this.cbxGrade.Value.AsInt();
                TS_TeachMaterials_Logic.TS_TeachMaterials_Add(MaterialsEntity);
            }
            Response.Redirect("TeachMaterialsManager.aspx");
        }

        protected void btnBack_Click(object sender, EventArgs e) {
            Response.Redirect("TeachMaterialsManager.aspx");
        }
    }
}